<?php
/**
 * Created by PhpStorm.
 * User: roy
 * Date: 16-8-8
 * Time: 上午11:07
 */

namespace App\Common;

use App\User;

if (!session_id()){
    session_start();
}
class Func
{

    /**
     * 判断用户是否登录
     */
    public static function isLogin()
    {
        if (empty($_SESSION['tianqi_userid'])){
            echo '<script>alert("你还没有登录");window.top.location.href="/login";</script>';exit;
        }
    }

    /**
     * 获取登录用户信息
     */
    public static function User()
    {
        if (!empty($_SESSION['tianqi_userid'])){
            $userid = $_SESSION['tianqi_userid'];
            $u = new User();
            $user = $u->get_usre(['id'=>$userid]);
            if ($user){
                $user = json_decode(json_encode($user), true);
            }
            return $user;
        }else{
            return null;
        }
    }

    /**
     * 发送短信
     */
    public static function send_sms($mobile,$content) {
        $url = 'http://www.sendcloud.net/smsapi/send';

        $param = array(
            'smsUser' => '***',
            'templateId' => '1',
            'phone' => $mobile,
            'vars' => "{\"%content%\":\"$content\"}"
        );

        $sParamStr = "";
        ksort($param);
        foreach ($param as $sKey => $sValue) {
            $sParamStr .= $sKey . '=' . $sValue . '&';
        }

        $sParamStr = trim($sParamStr, '&');
        $smskey = '***';
        $sSignature = md5($smskey."&".$sParamStr."&".$smskey);


        $param = array(
            'smsUser' => '***',
            'templateId' => '1',
            'phone' => $mobile,
            'vars' => "{\"%content%\":\"$content\"}",
            'signature' => $sSignature
        );

        $data = http_build_query($param);
        //echo $data;

        $options = array(
            'http' => array(
                'method' => 'POST',
                'header' => 'Content-Type:application/x-www-form-urlencoded',
                'content' => $data
            ));
        $context  = stream_context_create($options);
        $result = file_get_contents($url, FILE_TEXT, $context);
        return $result;
    }

}